package Border;

import SquarePackage.Direction;
import SquarePackage.Square;

/**
 * Class for doors in the dungeon.
 * 
 * @author 	Brecht
 * @version	1.0
 */
public class  Door extends NonEmptySpace{
	/**
	 * Initialize a new door.
	 */
	public Door(Square square, Direction direction){
		this(square,direction,false);
	}
	/**
	 * one square and door state is set.
	 */
	public Door(Square square, Direction direction, boolean open){
		this(square,null,direction,open);
	}
	/**
	 * extended constructor allows to set door state and 2 squares.
	 */
	public Door(Square square1, Square square2, Direction direction, boolean open){
		super(square1,square2,direction,false);
		setDoorState(open);
	}

	/**
	 * Variable registering whether this door is open or closed.
	 */
	private boolean open;
	
	/**
	 * Return whether this door is open or closed.
	 * @return	True if and only if the door is open
	 */
	public boolean isOpen() {
		return open;
	}
	
	/**
	 * Open or close the door.
	 * 
	 * @param	open
	 * 			The new state of the door.
	 */
	private void setDoorState(boolean open) {
		if(!isTerminated()){
		this.open = open;
		}
	}
	/**
	 * close a door
	 * @post isopen()=false
	 */
	public void close(){
		setDoorState(false);
	}
	/**
	 * opena door
	 * @post isOpen()=true
	 */
	public void open(){
		setDoorState(true);
	}
	/**
	 * Return whether this border is isolated.
     * @return	true if and only if the door is closed
	 */
	public boolean getIsolation(){
		return !isOpen();
	}
	/**
	 * Overriden method which checks if the direction is Floor or Wall, because this is not allowed for doors.
     * @return	True if and only if direction !=(null || CEILING || FLOOR)
	 */
	@Override
	public boolean isValidDirection(Direction direction) {
		return ((super.isValidDirection(direction))&& (direction != Direction.CEILING) && (direction != Direction.FLOOR));
	}
	/**
	 * Overridden method which chooses the preferred border
     * @return Border
     * 			this door if the other Border is not an instance of a high
	 */
	@Override
	public Border getHighest(Border border){
		if(border instanceof Door || border instanceof EmptySpace)
			return (Door)this;
		return border.getHighest(this);
	}
	/**
	 * Overridden method that specifies the conditions for a square to be used for a  door
	 * @return	true if and only if the square is valid and does not have a border in this direction yet, or the direction is not valid. and the square does not have more than 3 doors
	 */
	@Override
	public boolean canHaveAsSquare(Square square,Direction direction) {
		return (super.canHaveAsSquare(square, direction)) && !(square.amountOfDoors()>=3);

	}
    /**
     * can this type of border be on the floor?
     * @return false doors can not be in the floor
     */
    public boolean canBeOnFloor(){
    	return false;
    } 
    /**
     * can border be opened and closed?
     * @return True because this is a door
     */
    public boolean canBeOpenedOrClosed(){
    	return true;
    }
    /**
     * method to return an exact copy of this border needed for an exact copy of a square
     * @return Border
     */
    public Door getCopy(Square square, Direction direction){
    	return new Door(square,direction);
    }
    /**
     * 
     */
    @Override
    public String toString(){
    	return "" + super.toString() + " Is this Door open?: " +  this.isOpen();
    }
}